package com.itheima.stock.config;

import com.itheima.stock.pojo.vo.TaskThreadPoolInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class TaskExecutePoolConfig {
@Autowired
    private TaskThreadPoolInfo info;
@Bean(name = "threadPoolTaskExecutor",destroyMethod = "shutdown")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    //设置线程数
    executor.setCorePoolSize(info.getCorePoolSize());
    //设置最大线程数
    executor.setMaxPoolSize(info.getMaxPoolSize());
    //活跃时间
    executor.setKeepAliveSeconds(info.getKeepAliveSeconds());
    //任务队列长度
    executor.setQueueCapacity(info.getQueueCapacity());
//    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    executor.initialize();
    return executor;
}

}
